package com.catmiao.ai.config;

import com.catmiao.ai.assistant.ChatAssistant;
import com.catmiao.ai.assistant.ChatMemoryAssistant;
import com.catmiao.ai.prompt.LawAssistant;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.memory.chat.TokenWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.model.openai.OpenAiTokenCountEstimator;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LLMConfig {

    @Bean(name = "qwen")
    public ChatModel chatModelQwen(){
        String apiKey = System.getenv("BAILIAN_API_KEY");

        return OpenAiChatModel.builder()
                .apiKey(apiKey)
                .modelName("qwen-plus")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logResponses(true)
                .logRequests(true)
                .maxRetries(3) // 重试次数
                .build();
    }

    @Bean(name = "qwenLong")
    public ChatModel chatModelQwenLong(){
        String apiKey = System.getenv("BAILIAN_API_KEY");

        return OpenAiChatModel.builder()
                .apiKey(apiKey)
                .modelName("qwen-long")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logResponses(true)
                .logRequests(true)
                .maxRetries(3) // 重试次数
                .build();
    }


    /**
     * 流式对话接口
     */
    @Bean
    public StreamingChatModel streamingChatModel(){
        String apiKey = System.getenv("BAILIAN_API_KEY");

        return OpenAiStreamingChatModel.builder()
                .apiKey(apiKey)
                .modelName("qwen-plus")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logResponses(true)
                .logRequests(true)
                .build();
    }



    @Bean(name = "chatAssistant")
    public ChatAssistant chatAssistant(StreamingChatModel streamingChatModel){
        return AiServices.create(ChatAssistant.class, streamingChatModel);
    }

    @Bean(name = "chatMemoryWithMaxMessagesAssistant")
    public ChatMemoryAssistant chatMemoryWithMaxMessagesAssistant(@Qualifier("qwenLong") ChatModel chatModel){

        return AiServices.builder(ChatMemoryAssistant.class)
                .chatModel(chatModel)
                //  根据memoryId 保留记忆
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(5))
                .build();
    }


    @Bean(name = "chatMemoryWithMaxTokensAssistant")
    public ChatMemoryAssistant chatMemoryWithMaxTokensAssistant(@Qualifier("qwenLong")ChatModel chatModel){

        OpenAiTokenCountEstimator openAiTokenCountEstimator = new OpenAiTokenCountEstimator("gpt-4");
        return AiServices.builder(ChatMemoryAssistant.class)
                .chatModel(chatModel)
                //  根据memoryId 保留记忆
                .chatMemoryProvider(memoryId -> TokenWindowChatMemory.withMaxTokens(1000,openAiTokenCountEstimator ))
                .build();
    }

    @Bean(name = "lawAssistant")
    public LawAssistant lawAssistant(StreamingChatModel streamingChatModel){
        return AiServices.create(LawAssistant.class, streamingChatModel);
    }
}
